package cn.swu.ghx.swu.library

import android.Manifest
import android.annotation.SuppressLint
import android.content.pm.PackageManager
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import androidx.activity.result.contract.ActivityResultContracts
import androidx.core.app.ActivityCompat
import androidx.drawerlayout.widget.DrawerLayout
import androidx.fragment.app.Fragment
import androidx.viewpager2.widget.ViewPager2
import cn.swu.ghx.swu.library.fragment.AcademyFragment
import cn.swu.ghx.swu.library.fragment.PhotoFragment
import cn.swu.ghx.swu.library.fragment.TeacherFragment
import cn.swu.ghx.swu.library.ui.FragmentAdapter
import com.elvishew.xlog.XLog
import com.google.android.material.appbar.MaterialToolbar
import com.google.android.material.bottomnavigation.BottomNavigationView
import com.google.android.material.navigation.NavigationView
import java.security.Permission
import kotlin.contracts.contract

class MainActivity : AppCompatActivity() {

    val permissionLauncher = registerForActivityResult(ActivityResultContracts.RequestPermission() ){
        if(it){
            XLog.d("权限运行")
        }else{
            XLog.w("未授权")
        }
    }

    @SuppressLint("MissingInflatedId")
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        if(ActivityCompat.checkSelfPermission(this,Manifest.permission.INTERNET) !=
            PackageManager.PERMISSION_GRANTED  ){
            permissionLauncher.launch(Manifest.permission.INTERNET)
        }


        val toolbar = findViewById<MaterialToolbar>(R.id.toolbar)

        val drawerLayer = findViewById<DrawerLayout>(R.id.drawer_layout)
        toolbar.setNavigationOnClickListener {
            drawerLayer.open()
        }
        val drawer = findViewById<NavigationView>(R.id.drawer)
        drawer.setNavigationItemSelectedListener {
            XLog.d("点击了抽屉菜单：%s",it.title)
            when(it.itemId){
                R.id.m1->{

                }
                else-> {

                }
            }
          true
        }

        //
        showFragment("academy")
        val bottom = findViewById<BottomNavigationView>(R.id.bottom)
        val pager = findViewById<ViewPager2>(R.id.viewpager)
        bottom.setOnItemSelectedListener {
            XLog.d("点击底部菜单：%s",it.title)
            when(it.itemId){
                R.id.frag_academy->{
                    //showFragment("academy")
                    pager.currentItem = 0
                }
                R.id.frag_teacher->{
//                    showFragment("teacher")
                    pager.currentItem = 1
                }
                R.id.frag_photo->{
//                    showFragment("photo")
                    pager.currentItem = 2
                }
            }
            true
        }

        // viewpager2
        val fragAdapter = FragmentAdapter(this)

        pager.apply {
            this.adapter = fragAdapter
            this.registerOnPageChangeCallback(object: ViewPager2.OnPageChangeCallback() {
                override fun onPageSelected(position: Int) {
                    //
                    bottom.selectedItemId = when(position) {
                        0->R.id.frag_academy
                        1->R.id.frag_teacher
                        else -> R.id.frag_photo
                    }
                }
            })
        }
    }

    private fun showFragment(tag: String) {
//        val manager = supportFragmentManager
//        val tran = manager.beginTransaction()
//        var frag = manager.findFragmentByTag(tag)
//
//        manager.fragments.forEach {
//            tran.hide(it)
//        }
//
//        if(frag != null){
//            tran.show(frag)
//        }else{
//            frag = createFragmentByTag(tag)
//            frag?.apply{
//                tran.replace(R.id.fragment_container,this,tag)
//            }
//        }
//        tran.commit()
    }

    private fun createFragmentByTag(tag: String): Fragment? {
        when(tag){
            "academy"-> return AcademyFragment()
            "teacher"-> return TeacherFragment()
            "photo"-> return PhotoFragment()
        }
        return null
    }
}